<template>
  <view class="auth-container">
    <!-- 微信登录容器 -->
    <view class="auth-section wechat-section">
      <text class="section-title">微信服务号登录</text>
      <button class="auth-button" @click="handleWechatLogin">立即授权</button>
      <text class="section-tip">请在微信内打开页面</text>
    </view>

    <!-- 数据操作容器 -->
    <view class="auth-section data-section">
      <text class="section-title">数据管理</text>
      <button class="auth-button" @click="saveData">保存到Vuex</button>
      <text class="section-tip">仅用于功能演示</text>
    </view>
  </view>
</template>

<script>
export default {
  methods: {
	  login(params, type) { //联网验证登录
	  	// console.log('执行登录开始----');
	  	console.log({params,type});
	  	//toLowerCase
	  	let action = 'loginBy' + type.trim().replace(type[0], type[0].toUpperCase())
	  	const uniIdCo = uniCloud.importObject("uni-id-co",{
	  		customUI:true
	  	})
	  	uniIdCo[action](params).then(result => {
	  		uni.showToast({
	  			title: '登录成功',
	  			icon: 'none',
	  			duration: 2000
	  		});
	  		// #ifdef H5
	  		console.log('uni-id-pages uniIdCo result',result);
	  		result.loginType = type
	  		// #endif
	  		mutations.loginSuccess(result)
	  	})
	  	.catch(e=>{
	  		uni.showModal({
	  			content: e.message,
	  			confirmText:"知道了",
	  			showCancel: false
	  		});
	  	})
	  	.finally(e => {
	  		if (type == 'univerify') {
	  			uni.closeAuthView()
	  		}
	  		uni.hideLoading()
	  	})
	  },
    saveData() {
      const userData = {
        name: '张三',
        age: 125,
        token: 'abc123'
      }
       login(params, type)
      // 调用action保存数据
      this.$store.dispatch('saveUserData', userData)
      
      console.log('数据已保存到Vuex')
      this.$router.push('pageB') // 跳转到页面B
    }
  }
}
</script>